package com.example.shop.controller;

import com.example.shop.domain.Goods;
import com.example.shop.domain.Result;
import com.example.shop.domain.User;
import com.example.shop.service.AgreementService;
import com.example.shop.service.GoodsService;
import com.example.shop.service.UserService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Tag(name = "后台管理相关", description = "这是描述")
@RestController
@RequestMapping("/back")
@Validated
@Slf4j
public class BackController {

    @Autowired
    private UserService userService;

    @Operation(summary = "查询所有用户")
    @GetMapping("/getAllUser")
    public Result getAllUser(@RequestParam String username) {
        User user=new User();
        user.setUsername(username);
        return Result.success(userService.getAllUser(user));
    }



    @Autowired
    private GoodsService goodsService;

    @GetMapping("/list")
    @Operation(summary = "查询商品列表")
    public Result list(@RequestParam(value = "name", required = false) String name) {
        // 如果 name 参数为空，则将其设为 null 或空字符串，以便在服务层进行处理
        String queryName = (name != null && !name.isBlank()) ? name : null;
        return Result.success(goodsService.selectAllByName(queryName));
    }



    @DeleteMapping("/delete")
    @Operation(summary = "删除")

    public Result delete(@RequestBody Goods goods) {
//        安全校验
//        List<Goods> list = goodsService.selectById(goods);
//        if (list.isEmpty()) {
//            return Result.error("此账户中不存在该记录");
//        }
        goodsService.delete(goods);
        return Result.success();
    }

    @Autowired
    private AgreementService agreementService;
    @GetMapping("/agreement")
    @Operation(summary = "查询订单列表")
    public Result agreement(@RequestParam String name) {
        String queryName = (name != null && !name.isBlank()) ? name : null;
        return Result.success(agreementService.selectList(queryName));
    }


}
